'
'   XC Engine. A simple engine to drive the Internet Explorer web browser
'   Copyright (C) 2009 Stanislav Hordiyenko
'
'   This program is free software; you can redistribute it and/or modify
'   it under the terms of the GNU General Public License as published by
'   the Free Software Foundation; either version 3 of the License, or
'   (at your option) any later version.
'
'   This program is distributed in the hope that it will be useful,
'   but WITHOUT ANY WARRANTY; without even the implied warranty of
'   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'   GNU General Public License for more details.
'
'   You should have received a copy of the GNU General Public License
'   along with this program; if not, see http://www.gnu.org/licenses
'   or write to the Free Software Foundation,Inc., 51 Franklin Street,
'   Fifth Floor, Boston, MA 02110-1301 USA
'

Class Page
	Private objBrowser
	Private objHtmlDocument
	Private strHtmlDocumentUniqueId
	
	Public Sub Class_Initialize()
		Set objHtmlDocumentDocument = Nothing
	End Sub
	
	Public Sub Class_Terminate()
		Set objHtmlDocumentDocument = Nothing
	End Sub
	
	Private Function IsDocument(ByVal objHtmlElement)
		IsDocument = False
		
		If UCase(TypeName(objHtmlElement)) = "HTMLDOCUMENT" Then
			IsDocument = True
		End If
	End Function
	
	Public Sub WrapElement(ByVal objHtmlElement)
		If Not (objHtmlElement Is Nothing) Then
			If UCase(TypeName(objHtmlElement)) = "IE" Then
				If IsDocument(objHtmlElement.Object.Document) Then
					strHtmlDocumentUniqueId = objHtmlElement.Object.Document.Body.UniqueId

					Set objHtmlDocument = objHtmlElement.Object.Document
					Set objBrowser = objHtmlElement
					
					Exit Sub
				End If
			End If
		End If

		Err.Raise 10001, "Document->WrapElement", "The Html Element couldn't be wrapped"
	End Sub
	
	Public Sub Synchronize()
		On Error Resume Next

		Do Until objBrowser.Object.ReadyState = 4
			WScript.Sleep 10
		Loop

		Do Until objBrowser.Object.Document.ReadyState = "complete"
			WScript.Sleep 10
		Loop

		Set colFrames = objBrowser.Object.Document.GetElementsByTagName("IFRAME")
		
		iFramesCount = colFrames.Length

		For i = 0 To iFramesCount - 1		
			Do Until colFrames(i).ContentWindow.Document.ReadyState = "complete" Or _
			colFrames(i).ContentWindow.Document.ReadyState = "interactive"
				If Err.Number <> 0 Then
					Err.Clear
				
					Exit Do
				End If
				
				WScript.Sleep 10
			Loop
		Next

		Set colFrames = objBrowser.Object.Document.GetElementsByTagName("FRAME")

		iFramesCount = colFrames.Length

		For i = 0 To iFramesCount - 1
			Do Until colFrames(i).ContentWindow.Document.ReadyState = "complete" Or _
			colFrames(i).ContentWindow.Document.ReadyState = "interactive"
				If Err.Number <> 0 Then
					Err.Clear
				
					Exit Do
				End If
			
				WScript.Sleep 10
			Loop
		Next

		On Error Goto 0
	End Sub
	
	Public Function GetProperty(ByVal strPropertyName)
		If Exists() Then
			If LCase(strPropertyName) = "url" Then
				GetProperty = objHtmlDocument.Url
				
				Exit Function
			End If
		
			If Internal_GetProperty(objHtmlDocument.Body, strPropertyName, varPropertyName) Then
				GetProperty = varPropertyName
			Else
				Err.Raise 10003, "Document->GetProperty", "The Document doesn't support this property"
			End If
		Else
			Err.Raise 10002, "Document->GetProperty", "The Document doesn't exist"
		End If
	End Function
	
	Public Function IsReloaded()
		IsReloaded = False
		
		Call Synchronize()

		If strHtmlDocumentUniqueId <> objHtmlDocument.Body.UniqueId Then
			strHtmlDocumentUniqueId = objHtmlDocument.Body.UniqueId
			
			IsReloaded = True
		End If
	End Function
	
	Public Function ChildElements(ByVal strElementProperties)
		If Not Exists() Then
			Err.Raise 10002, "Document->ChildElements", "The Document doesn't exist"
		Else
			Set objElementsCollection = New ElementsCollection
		
			Call Internal_FilterElements(objHtmlDocument, strElementProperties, objElementsCollection)
			
			Set ChildElements = objElementsCollection
		End If
	End Function
	
	Public Sub FireEvent(ByVal strEventName)
		If Exists() Then
			objHtmlDocument.Body.FireEvent strEventName
		Else
			Err.Raise 10002, "Document->FireEvent", "The Document doesn't exist"
		End If
	End Sub
	
	Public Function Element()
		Set Element = Nothing
		
		If Not Exists() Then
			Err.Raise 10002,  "Document->Element", "The Document doesn't exist"
			
			Exit Function
		End If
		
		Set Element = objHtmlDocument
	End Function
	
	Public Function Exists()
		On Error Resume Next
		
		Exists = False

		If Not IsDocument(objHtmlDocument) Then Exit Function

		strHtmlElementTagName = objHtmlDocument.Body.TagName
		
		If Err.Number = 0 Then
			Exists = True
			
			Exit Function
		End If
		
		Err.Clear
		
		On Error Goto 0
	End Function
End Class